<?php
class ueditorController extends accessController{
    public static function _right(){
        return array(
            array('编辑器相关',1),
            array('图片上传','imageup'),
            array('附件上传','fileup'),
            array('在线管理','imagemanager'),
        );
    }
    
    private $upload_config=null;
    private $table=null;
    public function __construct(){
        parent::__construct();
		$this->table=table('uplog');
        $this->upload_config=$this->conf->ADMIN_UPLOAD;
		
    }
    public function index(){
        
    }
    //上传日志
    private function uplog($data){
        if(!is_array($data)) return false;
        $data['uniq']=md5(time().rand(100000,999999));
        $data['add_time']=time();
        $query="insert into `{$this->table}` 
        (`uniq`,`fileurl`,`filesize`,`fileext`,`kind`,`add_time`) 
        values (:uniq,:fileurl,:filesize,:fileext,:kind,:add_time)";
		$this->m->query($query,array('uniq'=>$data['uniq'],'fileurl'=>$data['fileurl'],'filesize'=>$data['filesize'],'fileext'=>$data['fileext'],'kind'=>$data['kind'],'add_time'=>$data['add_time']));
		return intval($this->m->insert_id());
    }

    //图片上传
    public function imageup($type='echo'){
        $title = htmlspecialchars($_POST['pictitle'], ENT_QUOTES);
        $path = htmlspecialchars($_POST['dir'], ENT_QUOTES);
        $up=new Uploader("upfile",$this->upload_config['image']);
        $info = $up->getFileInfo();
        if($info['state']=='SUCCESS'){
            $insert_id=$this->uplog(array('kind'=>'image','fileurl'=>$info['url'],'filesize'=>$info['size'],'fileext'=>$info['type']));
			$info['insert_id']=$insert_id;
		}
		$info['url_base']=$info['url'];
        $info['url']=$this->upload_config['image']['httpPath'].$info['url'];
        if($type=='echo'){
			echo json_encode($info);
		}else{
			return $info;
		}
        /*
        {"originalName":"380420.jpg","name":"13805294398235.jpg","url":"http:","size":32190,"type":".jpg","state":"SUCCESS"}
        */
    }
    //附件上传
    public function fileup($type='echo'){
        $up=new Uploader("upfile",$this->upload_config['file']);
        $info = $up->getFileInfo();
        if($info['state']=='SUCCESS'){
            $insert_id=$this->uplog(array('kind'=>'file','fileurl'=>$info['url'],'filesize'=>$info['size'],'fileext'=>$info['type']));
			$info['insert_id']=$insert_id;
		}
		$info['url_base']=$info['url'];
        $info['url']=$this->upload_config['file']['httpPath'].$info['url'];
        if($type=='echo'){
			echo json_encode($info);
		}else{
			return $info;
		}
    }
    //文件管理
    public function imagemanager(){
        //需要遍历的目录列表，最好使用缩略图地址，否则当网速慢时可能会造成严重的延时
        
        $paths = array($this->upload_config['image']['savePath']);
    
        $action = htmlspecialchars( $_POST[ "action" ] );
        if ( $action == "get" ) {
            $files = array();
            foreach ( $paths as $path){
                $tmp = $this->getfiles( $path , $this->upload_config['image']['httpPath'] );
                if($tmp){
                    $files = array_merge($files,$tmp);
                }
            }
            if ( !count($files) ) return;
            rsort($files,SORT_STRING);
            $str = "";
            foreach ( $files as $file ) {
                $str .= $file . "ue_separate_ue";
            }
            echo $str;
        }
    }

    /**
     * 遍历获取目录下的指定类型的文件
     * @param $path
     * @param array $files
     * @return array
     */
    private function getfiles( $path,$url , &$files = array() )
    {
        if ( !is_dir( $path ) ) return null;
        $handle = opendir( $path );
        while ( false !== ( $file = readdir( $handle ) ) ) {
            if ( $file != '.' && $file != '..' ) {
                $path2 = $path . '/' . $file;
                
                if ( is_dir( $path2 ) ) {
                    $url2=$url.$file.'/';
                    $this->getfiles( $path2,$url2 , $files );
                } else {
                    $url2=$url.$file;
                    if ( preg_match( "/\.(gif|jpeg|jpg|png|bmp)$/i" , $file ) ) {
                        $files[] = $url2;
                    }
                }
            }
        }
        return $files;//改为返回http://
    }

    
    
    
    
}